// pages/search/search.js
//获取应用实例
const app = getApp()
const util = require("../../utils/util.js");
var page = 1;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        searchList: [],
        servList: [],
        bsyList: [],
        keywords: '',
        isHiddenHistory: false, // 历史搜索
        historyList: [], // 历史记录数组
        showSearch: true,
        showtk: false,
        // cityname: app.globalData.extConfig.cityname,
        // citycode: app.globalData.extConfig.citycode,

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            citycode: options.city ? options.city : (ks.getStorageSync("citycode") ? ks.getStorageSync("citycode") : 'sz'),
        })

        if (options.keywords) {
            this.getServiceData(options.keywords);
            this.getinfoData(options.keywords);
            this.getbsyData(options.keywords);
            this.setData({
                keywords: options.keywords
            })
        }

        util.getUrl(app.globalData.url).then((res) => {
            util.cityList().then((res) => {
                let citydata = res.allcity.filter(v => v.citycode == this.data.citycode);
                if (citydata)
                    this.setData({
                        city: citydata[0].cityname
                    })
            })
            this.getSearchList();
            this.getHotWords();
        })
    },

    // 搜索的历史记录
    getSearchList() {
        let data = {
            action: "search",
            option: "list",
            version: app.globalData.version,
            openid: ks.getStorageSync("openid"),
            user_id: ks.getStorageSync("user_id"),
        };
        util.request('user.php', data, 'POST').then(res => {
            this.setData({
                historyList: res.data.data,
            })
            if (res.data.state == 1) {
                this.setData({
                    isHiddenHistory: true,
                })
            }
        })
    },

    // 添加搜索词
    getAddKeywords(keywords) {
        let data = {
            action: "search",
            option: "add",
            openid: ks.getStorageSync("openid"),
            user_id: ks.getStorageSync("user_id"),
            citycode: this.data.citycode,
            value: keywords,
            version: app.globalData.version,
        };
        util.request('user.php', data, 'POST')
    },

    // 热词搜索接口
    getHotWords() {
        let hotData = {
            action: "hotwords",
            city: ks.getStorageSync("city"),
            version: app.globalData.version,
        };
        util.request('service.php', hotData, ).then(res => {
            this.setData({
                hotList: res.data.data,
            })
        })
    },

    // 获取服务搜索结果
    getServiceData(keywords) {
        let serData = {
            action: "tools",
            city: this.data.city,
            keywords: keywords,
            type: "search",
            version: app.globalData.version,
        };
        util.request('service.php', serData, ).then(res => {
            if (res.data.data)
                this.setData({
                    servFlag: res.data.state,
                    servList: res.data.data.slice(0, 3),
                    servLength: Object.keys(res.data.data).length,
                })
        })
    },

    // 资讯搜索结果
    getinfoData(keywords) {
        let searchData = {
            citycode: this.data.citycode,
            action: 'list',
            pagesize: 10,
            keywords: keywords,
            // page,
            version: app.globalData.version,

        };
        util.request('index.php', searchData, 'GET').then(res => {
            this.setData({
                searchList: res.data.slice(0, 3),
                searchLength: res.data.length,
            })
        })
    },

    // 办事搜索结果
    getbsyData(keywords) {
        let searchData = {
            citycode: this.data.citycode == "sz" ? "bsy" : this.data.citycode,
            action: 'list',
            pagesize: 10,
            bsy: 1,
            keywords: keywords,
            // page,
            version: app.globalData.version,

        };
        util.request('index.php', searchData, 'GET').then(res => {
            this.setData({
                bsyList: res.data ? res.data.slice(0, 3) : '',
                bsyLength: res.data ? res.data.length : '',
            })
        })
    },

    inputTap(e) {
        let keywords = e.detail.value;
        this.setData({
            keywords,
            searchList: [],
            servList: [],
            bsyList: [],
            showSearch: true,
        })
    },

    // 输入框回车搜索事件
    confirmTap(e) {
        let keywords = e.detail.value;
        this.setData({
            keywords,
            searchList: [],
            showSearch: false,
        })
        this.getAddKeywords(keywords)
        this.getServiceData(keywords);
        this.getinfoData(this.data.keywords);
        this.getbsyData(keywords);
    },

    // 输入框删除icon事件
    delTap() {
        this.setData({
            keywords: "",
            showSearch: true,
            searchList: [],
            servList: [],
            bsyList: [],
        })
    },

    // 清除历史记录事件
    onDelhistory: function(e) {
        let data = {
            action: "search",
            option: "del",
            openid: ks.getStorageSync("openid"),
            user_id: ks.getStorageSync("user_id"),
        };
        util.request('user.php', data, 'POST')
        this.setData({
            // historyList: [],
            isHiddenHistory: false
        })
    },

    // 点击历史记录
    bindhisNav: function(e) {
        var keywords = e.currentTarget.dataset.name;
        this.getServiceData(keywords);
        this.getinfoData(keywords);
        this.getbsyData(keywords);
        this.setData({
            showSearch: false,
            keywords,
        })
    },

    // 热词点击事件
    hotwordsBtn(e) {
        let keywords = e.currentTarget.dataset.value;
        this.getServiceData(keywords);
        this.getinfoData(keywords);
        this.getbsyData(keywords)
        this.setData({
            showSearch: false,
            keywords
        })
    },

    // 工具、服务点击事件
    servBtn(e) {
        let path = e.currentTarget.dataset.path;
        let appid = e.currentTarget.dataset.appid;
        let linkname = e.currentTarget.dataset.title;
        if (path.indexOf("pages") == -1 && path.indexOf("mp.weixin.qq.com") == -1) {
            this.setData({
                showtk: true
            })
            util.navContact(linkname, path)
        } else {
            util.navMethod(path, appid);
        }
    },

    // 进入客服会话
    contact() {
        this.setData({
            showtk: false,
        })
        let replay = {
            action: "reply",
            title: this.data.linkname,
            url: this.data.path,
            openid: ks.getStorageSync("openid")
        }
        util.request('user.php', replay, 'POST')
    },

    // 关闭
    close() {
        this.setData({
            showtk: false,
        })
    },



    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        // page++;
        // this.getinfoData(this.data.keywords, page);
    },
})